package com.traveldesign.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traveldesign.common.JwtClaimsConstant;
import com.traveldesign.common.QueryPageParam;
import com.traveldesign.common.Result;
import com.traveldesign.entity.Menu;
import com.traveldesign.entity.Post;
import com.traveldesign.entity.User;
import com.traveldesign.service.IMenuService;
import com.traveldesign.service.IPostService;
import com.traveldesign.service.IUserService;
import com.traveldesign.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangwang
 * @since 2025-01-20
 */
@RestController
// 允许所有来源的跨域请求（适用于开发环境）
//@CrossOrigin(origins = "*")
@RequestMapping("/post")
public class PostController {
    private static final Logger log = LoggerFactory.getLogger(PostController.class);
    //    @GetMapping
//    public  String hello(){
//        return "hello travel";
//    }
    @Resource
    private IPostService postService;
    //    查询
    @GetMapping("/list")
    public List<Post> list(){
        return  postService.list();
    }

    //    新增
    @PostMapping("/save")
    public  Result save(@RequestBody Post post){

        return postService.save(post)?Result.suc():Result.fail();
    }
    //     修改
    @PostMapping("/update")
    public   Result update(@RequestBody Post post){
        return postService.updateById(post)?Result.suc():Result.fail();
    }
    @GetMapping("/del")
    public   Result del(@RequestParam String id){
        return postService.removeById(id)?Result.suc():Result.fail();
    }
    // 查询（模糊like、匹配eq（等于））
    @PostMapping("/listP")
    public  Result listP(@RequestBody Post post){
        LambdaQueryWrapper<Post> lambdaQueryWrapper = new LambdaQueryWrapper();

        if(StringUtils.isNotBlank(post.getTitle())){
            lambdaQueryWrapper.like(Post::getTitle, post.getTitle());
        }
        return Result.suc(postService.list(lambdaQueryWrapper));
    }
//    @PostMapping("/listPageC1")
//    public Result listPageC1(@RequestBody QueryPageParam query){
//        HashMap param = query.getParam();
//        String name = (String) param.get("name");
//        String sex  =(String) param.get("sex");
//        String roleId  =(String) param.get("roleId");
//
//        System.out.println("name==="+(String)param.get("name") );
//
//        Page<User> page = new Page();
//        page.setCurrent(query.getPageNum());
//        page.setSize(query.getPageSize());
//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
//            lambdaQueryWrapper.like(User::getName,name);
//        }
//        if(StringUtils.isNotBlank(sex)){
//            lambdaQueryWrapper.eq(User::getSex,sex);
//        }
//        if(StringUtils.isNotBlank(roleId)){
//            lambdaQueryWrapper.eq(User::getRoleId,roleId);
//        }
////        IPage result = userService.pageC(page);
//        IPage result = userService.pageCC(page,lambdaQueryWrapper);
//        System.out.println("total==="+result.getTotal());
//
//        return  Result.suc(result.getRecords(),result.getTotal());
//    }
}
